package com.ly.mybatis.dynamicsql;

import com.ly.mybatis.entity.Meal;
import com.ly.mybatis.service.MealService;
import com.ly.mybatis.service.impl.MealServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname TestDynamicSql
 * @Description TODO
 * @Date 2021/1/20 16:12
 * @Author 冷心影翼
 */
@SpringBootTest
public class TestDynamicSql {

    @Autowired
    private MealService mealService;

    @Test
    public void testWhereIf() {
        String name = "aaa";
        Integer type = 2;
        Double priceMin = 500d;
        Double pricceMax = 800d;
        List<Meal> list = mealService.selectByMealParamter(name,type,priceMin,pricceMax);
        List<Meal> list2 = mealService.selectByMealParamter(null,type,priceMin,pricceMax);
        List<Meal> list3 = mealService.selectByMealParamter(null,type,priceMin,pricceMax);
        List<Meal> list4 = mealService.selectByMealParamter(null,null,priceMin,pricceMax);
        List<Meal> list5 = mealService.selectByMealParamter(null,null,null,pricceMax);
        List<Meal> list6 = mealService.selectByMealParamter(null,null,null,null);
        list.forEach(System.out::println);
        System.out.println("------------");
        list2.forEach(System.out::println);
        System.out.println("------------");
        list3.forEach(System.out::println);
        System.out.println("------------");
        list4.forEach(System.out::println);
        System.out.println("------------");
        list5.forEach(System.out::println);
        System.out.println("------------");
        list6.forEach(System.out::println);
    }

    @Test
    public void testWhenChooseOtherwise() {
       List<Meal> list =  mealService.selectByType(0);
       List<Meal> list2 =  mealService.selectByType(1);
       List<Meal> list3 =  mealService.selectByType(-999);
       list.forEach(System.out::println);
       System.out.println("------------");
       list2.forEach(System.out::println);
       System.out.println("------------");
       list3.forEach(System.out::println);
       System.out.println("------------");
    }

    @Test
    public void testForeach() {
        List<Integer> types = new ArrayList<>();
        types.add(1);
        types.add(2);
        types.add(3);
        List<Meal> list = mealService.selectByTypes(types);
        list.forEach(System.out::println);
        System.out.println("-----");
    }
}
